﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STX.Repository;
using STX.Service;
namespace STX.Service.Valid
{
    public abstract class ValidExpression<TEntity> : IValidExpression<TEntity>
        where TEntity : STX.Domain.BaseEntity
    {
        public ITransactionContext Transaction { get; set; }

        public ValidationResult Valid(TEntity entity)
        {
            var result = new ValidationResult();
            ValidEntity(result, entity);
            return result;
        }

        public abstract void ValidEntity(ValidationResult result, TEntity entity);

        protected void PushError(ValidationResult result, String errorMsg)
        {
            result.Errors.Add(errorMsg);
        }

        protected void NoEmpty(ValidationResult result, String name, String value)
        {
            if (value.IsNullOrEmpty())
            {
                PushError(result, name + "为必填项");
            }
        }
    }
}
